/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iv1201.view;

import iv1201.controller.Controller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

/**
 *
 * @author Mustafa
 */

@Named(value = "userManager")
@SessionScoped
public class UserManager implements Serializable 
{
    
    private static final long serialVersionUID = 0xE9085B8530336BE1L;
    
    // The EJB annotation tells that the controller is an EJB
    @EJB
    private Controller controller;
    
    // variables that defines a person in the Database
    private int person_id;
    private String username;
    private String password;    
    private String name;
    private String surname;
    private String email;
    private String ssn;
    private int role_id;
   
    /*
     * Variables for the second stage of the job application 
     * (areas & years of experience)
     */
    private String areas;
    private int years;
    
    /*
     * Variables for the third stage of the job application 
     * (Periods of working in each area)
     */
    private Date dateFieldTo;
    private Date dateFieldFrom;
    
    /*
     * Set methods that represent a person with all the attributes from
     * name to password.
     */
    public void setPerson_id(int person_id)
    {
        this.person_id = person_id;
    }    
    
    public void setUsername(String username)
    {
        this.username = username;
    }
       
    public void setPassword(String password)
    {
        this.password = password;
    }
    
     public void setName (String name)
    {
        this.name = name;
    }
     
    public void setSurname(String surname)
    {
        this.surname = surname;
    }
 
    public void setEmail(String email)
    {
        this.email = email;
    }
    
    public void setSsn (String ssn)
    {
        this.ssn = ssn;
    }
    
    public void setRole_id(int role_id)
    {
        this.role_id = role_id;
    }
    
    
    /*
     * setters for different areas of experience together with the years in
     * each area.
     */
    public void setAreas(String areas)
    {
        this.areas = areas;
    }

    public void setYears(int years)
    {
        this.years = years;
    } 

    /*
     * Set methods for seting periods in which the job applicant wants to work
     * Relevant in the third stage of the job application
     */
    public void setDateFieldTo(Date dateFieldTo) 
    {
       this.dateFieldTo = dateFieldTo;
    }
    
    public void setDateFieldFrom(Date dateFieldFrom) 
    {
       this.dateFieldFrom = dateFieldFrom;
    }

    
    /*
     * Get methods that represent a person with all the attributes from
     * name to password
     */
    public String getUsername()
    {
        return username;
    }        
    
    public String getPassword()
    {
        return password;
    }  
    
    public int getPerson_id()
    {
        return person_id;
    }
    
    public String getName()
    {
        return name;
    }
    
    public String getSurname()
    {
        return surname;
    }

    public String getEmail()
    {
        return email;
    } 
 
    public String getSsn()
    {
        return ssn;
    }  

    public int getRole_id()
    {
        return role_id;
    } 

    
    /*
     * Get methods for different areas of experience together with the years in
     * each area.
     */
    public String getAreas()
    {
        return areas;
    }             

    public int getYears()
    {
        return years;
    }
    
    
    /*
     * Get methods for geting periods in which the job applicants wants to work.
     * Relevant in the third stage of the job application
     */
    public Date getDateFieldFrom()
    {
       return dateFieldFrom;
    }        
    
    public Date getDateFieldTo()
    {
       return dateFieldTo;
    }
    
    public List<DateField> getDates()
    {
        return dateFields;
    }
    
    
    /*
     * Public list called getItems that only receives objects of the type Item.
     * Item is defined in the class Item in the View.
     * Returns both the area with its corresponding years as ONE object.
     */
    public List<Item> getItems()
    {
        return areaYears;
    }   
    
    // Initialize the list.
    List<Item> areaYears = null;
    
    // method for adding to the list getItems
    public void addItem()
    {
        // If there is something to add, create a new list called
        // areaYears & add an "item" (areas + years)
        if (areaYears == null)
            areaYears = new ArrayList<Item>();
        
        // Create an item consisting of areas & years from the class Item
        areaYears.add(new Item(areas, years)); 
        
    } 
    
    // Method for clearing the whole experience list
    public void clearItems()
    {
        years = 0;
        areaYears.clear();
    }      
    
   /*
    * new list for adding start & end date for preferable working periods.
    * Here, DateField is an item that contains both start & end dates defined
    * in DateFields, similar to the class Item used in the lists above.
    */
   List<DateField> dateFields = null;
    public void addDates()
    {
        if (dateFields == null)
            dateFields = new ArrayList<DateField>();
        dateFields.add(new DateField(dateFieldFrom, dateFieldTo));
    }

    // clear the whole date list
    public void clearDates()
    {
        dateFieldTo = null;
        dateFieldFrom = null;
        dateFields.clear();
    }     
    
    
     /*
     * Method of loging in. Needs to crossference with DB & therefore,
     * delegates operation to controller.
     */
    public String checkLogin()
    {
        String who = controller.loginRecruiter(username, password);
        
        /*
         * Error handling - if the user wrote wrong user/password or if the
         * user left one of the fields empty, Redirect to corresponding 
         * error page.
         * 
         * The string in each "something.equals ("") comes from EntityDAO 
         * that returns a specific "error" string depending on the error made 
         * by the user that is trying to log in.
         */
        
        if (who.equals("user_pw"))
        {
            username = "";
            return "error_both.xhtml"; 
        }
        
        else if (who.equals("user"))
        {
            return "error_user.xhtml";
        }
        
        else if (who.equals("pw"))
        {
            username = "";
            return "error_psw.xhtml";
        }
        
        /*
         * If the recruiter entered the right username & password the user will
         * continue to the welcome page. If not - the user will be
         * redirected to corresponding error page
         */
        else if (who.equals("wrong"))
        {
            username = "";      
            return "error_login.xhtml";                    
        }
        
        else
        {
            return "welcome.xhtml";
        }
    }
    
    /*
     * Gets the name of the user who's logged in & sets it at the top of the 
     * welcome page.
     */
    public String welcomeLogin()
    {
        return controller.welcomeLogin(username);
    }
    
    /*
     * Error handler which makes sure that no fields are empty when the
     * applicant is filling the first application form. 
     */
    public String checkInfo()
    {
        String check = controller.applicantInfo(name, surname, email);
        
        if (check.equals("wrong"))
        {
            name = "";
            surname = "";
            email = "";
            return "error_apply.xhtml"; 
        }
        
        else
        {
            return "apply_xp.xhtml";
        }
    }    
    
    
    /*
     * Method to submit the application.  
     */
    public void next()
    {
       // empty ATM 
    }
    
}
